package com.shujia.spark.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo10Sort {

  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf()
      .setAppName("sort")
      .setMaster("local")

    val sc = new SparkContext(conf)

    val listRDD: RDD[Int] = sc.parallelize(List(1,3,2,5,4,8,6,7,9))

    /**
      * sortBy 指定一个列进行排序，默认升序,-i降序
      */

    val sortByRDD: RDD[Int] = listRDD.sortBy(i => -i)

    sortByRDD.foreach(println)

    /**
      * sortByKey 通过key来进行排序
      */

    val listRDD2: RDD[(String, Int)] = sc.parallelize(List(("java",22),("hadoop",15),("scala",21)))

    val sortByKeyRDD: RDD[(String, Int)] = listRDD2.sortByKey()

    val sortByKeyRDD2: RDD[(String, Int)] = listRDD2.sortBy(i => i._2)

    sortByKeyRDD2.foreach(println)

  }

}
